function generateMfuncModel_DSGEforsight_N_file(allModelParams,x1,y,strucOfArrays,positionFile,nameOfFunction)
% This function generates an M function which computes F-function of the model
% as needed to solve the DSGE model by the Extended Path, 
% The results are saved in the file nameOfFunction, which must be
% nameOfFunction = 'DSGEforsight_N.m';

% We start deleting the old version of the file - if it exists
saveFile = [positionFile,'\',nameOfFunction];
if exist(saveFile,'file') > 0
    delete(saveFile)
end

text = ['function F = ',nameOfFunction(1:end-2),'(Z,x_t,y_tN,N,setupEPer)'];
dlmwrite(saveFile,text,'-append','delimiter','');
text = ' ';
dlmwrite(saveFile,text,'-append','delimiter','');
text = '%Unfold some elements in setupEper';
dlmwrite(saveFile,text,'-append','delimiter','');
text = 'nx        = setupEPer.nx;';
dlmwrite(saveFile,text,'-append','delimiter','');
text = 'mx        = setupEPer.mx;';
dlmwrite(saveFile,text,'-append','delimiter','');
text = 'myx       = setupEPer.myx;';
dlmwrite(saveFile,text,'-append','delimiter','');
text = 'ny        = setupEPer.ny;';
dlmwrite(saveFile,text,'-append','delimiter','');
text = 'params    = setupEPer.params;';
dlmwrite(saveFile,text,'-append','delimiter','');
text = 'hx        = setupEPer.hx;';
dlmwrite(saveFile,text,'-append','delimiter','');
text = ' ';
dlmwrite(saveFile,text,'-append','delimiter','');
text = '%Unfold params';
dlmwrite(saveFile,text,'-append','delimiter','');
for i=1:length(allModelParams)
    text = [allModelParams{i}, '= params.',allModelParams{i},';'];
    dlmwrite(saveFile,text,'-append','delimiter','');
end 
text = ' ';
dlmwrite(saveFile,text,'-append','delimiter','');
text = '% Retrieving stacked variables';
dlmwrite(saveFile,text,'-append','delimiter','');
text = '[x,y] = VarStack(Z,nx,mx,myx,ny,hx,x_t,y_tN,N);';
dlmwrite(saveFile,text,'-append','delimiter','');
text = ' ';
dlmwrite(saveFile,text,'-append','delimiter','');
text = '% Adding the level to the variables';
dlmwrite(saveFile,text,'-append','delimiter','');
text = 'y = y + repmat(transpose(setupEPer.g0),N+1,1);';
dlmwrite(saveFile,text,'-append','delimiter','');
text = 'x = x + repmat(transpose(setupEPer.h0),N+1,1);';
dlmwrite(saveFile,text,'-append','delimiter','');
text = ' ';
dlmwrite(saveFile,text,'-append','delimiter','');
text = '% Current values in the model;';
dlmwrite(saveFile,text,'-append','delimiter','');
text = '% x(N,mx) y(N,ny);';
dlmwrite(saveFile,text,'-append','delimiter','');
text = '% x;';
dlmwrite(saveFile,text,'-append','delimiter','');
for i=1:length(x1)
     text = [char(x1(i)),' = x(1:N,', num2str(i),');';];
     dlmwrite(saveFile,text,'-append','delimiter','');
end
text = '% xp;';
dlmwrite(saveFile,text,'-append','delimiter','');
for i=1:length(x1)
     text = [char(x1(i)),'p',' = x(2:N+1,', num2str(i),');',];
     dlmwrite(saveFile,text,'-append','delimiter','');
end
text = ' ';
dlmwrite(saveFile,text,'-append','delimiter','');
text = '% y';
dlmwrite(saveFile,text,'-append','delimiter','');
for i=1:length(y)
     text = [char(y(i)),' = y(1:N,', num2str(i),');';];
     dlmwrite(saveFile,text,'-append','delimiter','');
end
text = '% yp;';
dlmwrite(saveFile,text,'-append','delimiter','');
for i=1:length(y)
     text = [char(y(i)),'p',' = y(2:N+1,', num2str(i),');',];
     dlmwrite(saveFile,text,'-append','delimiter','');
end
text = ' ';
dlmwrite(saveFile,text,'-append','delimiter','');
text = '%% Setting the dimension of the matrices ';
dlmwrite(saveFile,text,'-append','delimiter','');
text = 'f = zeros(ny+mx,N);';
dlmwrite(saveFile,text,'-append','delimiter','');
text = '%% f Function evaluation';
dlmwrite(saveFile,text,'-append','delimiter','');
DispSymMatrixMatlab_file_vector(strucOfArrays.('f'),'f',saveFile);
text = ' ';
dlmwrite(saveFile,text,'-append','delimiter','');
text = 'F = reshape(f,N*(ny+mx),1);';
dlmwrite(saveFile,text,'-append','delimiter','');
text = 'end';
dlmwrite(saveFile,text,'-append','delimiter','');
end





